// modaddress.js
var shihan = require('../../common/fetch')
var config = require('../../common/config')
var pages, prevPage;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showLoading: true,
    addresslist: []
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      check_address_id: options.id || '', //代理商进货页面带过来的id如果没有在地址列表里面，说明地址删除  delete_address设为1
      adtype: options.adtype || '',
      salesruleid: options.salesruleid || ''
    })
  },
  btnnewaddress: function(e) {
    var restype = e.currentTarget.dataset.type
    wx.navigateTo({
      url: '/pages/newaddress/newaddress?id=' + e.currentTarget.dataset.id + '&type=' + restype,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this
    that.refrashlist();
    pages = getCurrentPages();
    if (pages.length >= 2) {
      prevPage = pages[pages.length - 2];
      prevPage.setData({
        delete_address: 0
      })
    }
  },
  setdefault: function(e) {
    var that = this
    that.setData({
      showLoading: true
    })
    if (e.currentTarget.dataset.type == 0) { //设置默认地址
      shihan.sendRequest(config.set_address_default, 'POST', {
          id: e.currentTarget.dataset.id
        })
        .then(function(response) {
          that.refrashlist()
        }, function(error) {
          console.log(error);
        });
    } else { //取消默认地址
      shihan.sendRequest(config.cancel_address_default, 'POST', {
          id: e.currentTarget.dataset.id
        })
        .then(function(response) {
          that.refrashlist()
        }, function(error) {
          console.log(error);
        });
    }
  },
  refrashlist: function() {
    var that = this
    shihan.sendRequest(config.get_address_list, 'POST', {})
      .then(function(response) {
        if (response.data.code == 0) {
          that.setData({
            showLoading: false,
            addresslist: response.data.data.sh_items
          })
        } else {
          that.setData({
            showLoading: false
          })
        }
      }, function(error) {
        console.log(error);
      });
  },
  deleteaddress: function(e) {
    var that = this,
      flag = true;
    var id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    wx.showModal({
      title: "提示",
      content: '确定要删除改地址吗',
      confirmText: "确认",
      confirmColor: "#c40000",
      showCancel: true,
      success: function(res) {
        if (res.confirm) {
          that.setData({
            showLoading: true
          })
          shihan.sendRequest(config.remove_address, 'POST', {
              id: id
            })
            .then(function(response) {
              if (response.data.code == 0) {
                that.data.addresslist.splice(index, 1); //删除本地地址
                that.setData({
                  addresslist: that.data.addresslist
                })
                if (that.data.check_address_id) { //代理商进货页面带过来的id如果没有在地址列表里面，说明地址删除  delete_address设为1
                  for (var i = 0; i < that.data.addresslist.length; i++) {
                    if (that.data.addresslist[i].sh_id == that.data.check_address_id) {
                      flag = false;
                    }
                  }
                  if (flag) {
                    if (prevPage) {
                      prevPage.setData({
                        delete_address: 1
                      })
                    }
                  }
                }
                that.refrashlist();
              }
            }, function(error) {
              console.log(error);
            });
        }
      }
    })
  },
  selectaddress: function(e) {
    var that = this
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1]; //当前页面
    var prevPage = pages[pages.length - 2]; //上一个页面
    var index = e.currentTarget.dataset.index;
    if (that.data.adtype != 'mine') {
      wx.setStorage({
        key: 'selectaddresskey',
        data: that.data.addresslist[index],
        success: function() {
          //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
          prevPage.setData({
            salesruleid: that.data.salesruleid
          })
          wx.navigateBack()
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  }
})